<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>50行代码实现gan系列-pytorch | Jin Tian</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  
  
  
  <meta name="description" content="人生苦短我用GAN  首先声明一下，本教程面向入门吃瓜群众，大牛可以绕道，闲话不多说，先方一波广告。（高级GAN玩法），怎么说，我越来越感觉到人工智能正在迎来生成模型的时代，以前海量数据训练模型的办法有点揠苗助长，看似效果很好，实际上机器什么卵都没有学到（至少从迁移性上看缺少一点味道，不过就图片领域来说另当别论，在CV领域监督学习还是相当成功）。但是问题来了，GAN这么屌这么牛逼，我怎么搞？怎么">
<meta property="og:type" content="article">
<meta property="og:title" content="50行代码实现GAN系列-PyTorch">
<meta property="og:url" content="http://yoursite.com/2017/05/29/50行代码实现GAN系列-PyTorch/index.html">
<meta property="og:site_name" content="Jin Tian">
<meta property="og:description" content="人生苦短我用GAN  首先声明一下，本教程面向入门吃瓜群众，大牛可以绕道，闲话不多说，先方一波广告。（高级GAN玩法），怎么说，我越来越感觉到人工智能正在迎来生成模型的时代，以前海量数据训练模型的办法有点揠苗助长，看似效果很好，实际上机器什么卵都没有学到（至少从迁移性上看缺少一点味道，不过就图片领域来说另当别论，在CV领域监督学习还是相当成功）。但是问题来了，GAN这么屌这么牛逼，我怎么搞？怎么">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://wiseodd.github.io/img/2016-09-17-gan-tensorflow/algorithm.png">
<meta property="og:updated_time" content="2017-05-29T11:43:40.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="50行代码实现GAN系列-PyTorch">
<meta name="twitter:description" content="人生苦短我用GAN  首先声明一下，本教程面向入门吃瓜群众，大牛可以绕道，闲话不多说，先方一波广告。（高级GAN玩法），怎么说，我越来越感觉到人工智能正在迎来生成模型的时代，以前海量数据训练模型的办法有点揠苗助长，看似效果很好，实际上机器什么卵都没有学到（至少从迁移性上看缺少一点味道，不过就图片领域来说另当别论，在CV领域监督学习还是相当成功）。但是问题来了，GAN这么屌这么牛逼，我怎么搞？怎么">
<meta name="twitter:image" content="http://wiseodd.github.io/img/2016-09-17-gan-tensorflow/algorithm.png">
  
    <link rel="alternate" href="/atom.xml" title="Jin Tian" type="application/atom+xml">
  

  

  <link rel="icon" href="/css/images/mylogo.jpg">
  <link rel="apple-touch-icon" href="/css/images/mylogo.jpg">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>
  <script src="/js/bootstrap.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >

  
    <link rel="stylesheet" href="/css/dialog.css">
  

  

  
    <link rel="stylesheet" href="/css/header-post.css" >
  

  
  
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>



  <body data-spy="scroll" data-target="#toc" data-offset="50">


  
  <div id="container">
    <div id="wrap">
      
        <header>

    <div id="allheader" class="navbar navbar-default navbar-static-top" role="navigation">
        <div class="navbar-inner">
          
          <div class="container"> 
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>

            
              <a class="brand" style="
                 border-width: 0px;  margin-top: 0px;"  
                href="#" data-toggle="modal" data-target="#myModal" >
                  <img width="124px" height="124px" alt="Hike News" src="/css/images/mylogo.jpg">
              </a>
            
            
            <div class="navbar-collapse collapse">
              <ul class="hnav navbar-nav">
                
                  <li> <a class="main-nav-link" href="/">首页</a> </li>
                
                  <li> <a class="main-nav-link" href="/archives">归档</a> </li>
                
                  <li> <a class="main-nav-link" href="/categories">分类</a> </li>
                
                  <li> <a class="main-nav-link" href="/tags">标签</a> </li>
                
                  <li> <a class="main-nav-link" href="/about">关于</a> </li>
                
                  <li> <a class="main-nav-link" href="http://luoli-luoli.com/chat">chat</a> </li>
                
                  <li><div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="" />
        <button type="submit" class="search-form-submit"></button>
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="请输入关键词..." />
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(无标题)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>

</div></li>
            </div>
          </div>
                
      </div>
    </div>

</header>



      
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-50行代码实现GAN系列-PyTorch" style="width: 75%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      50行代码实现GAN系列-PyTorch
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	<a href="/2017/05/29/50行代码实现GAN系列-PyTorch/" class="article-date">
	  <time datetime="2017-05-29T09:16:22.000Z" itemprop="datePublished">2017-05-29</time>
	</a>

      
      
	<a class="article-views">
	<span id="busuanzi_container_page_pv">
		阅读量<span id="busuanzi_value_page_pv"></span>
	</span>
	</a>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <blockquote>
<p>人生苦短我用GAN</p>
</blockquote>
<p>首先声明一下，本教程面向入门吃瓜群众，大牛可以绕道，闲话不多说，先方一波广告。（高级GAN玩法），怎么说，我越来越感觉到人工智能正在迎来生成模型的时代，以前海量数据训练模型的办法有点揠苗助长，看似效果很好，实际上机器什么卵都没有学到（至少从迁移性上看缺少一点味道，不过就图片领域来说另当别论，在CV领域监督学习还是相当成功）。<br>但是问题来了，GAN这么屌这么牛逼，我怎么搞？怎么入门？谁带我？慌了！</p>
<h1 id="莫慌，50行代码你就可以成为无监督学习大牛"><a href="#莫慌，50行代码你就可以成为无监督学习大牛" class="headerlink" title="莫慌，50行代码你就可以成为无监督学习大牛"></a>莫慌，50行代码你就可以成为无监督学习大牛</h1><p>我最讨厌那些，嘴里一堆算法，算法实现不出来的人。因为我喜欢看到结果啊！尤其是一些教程，就是将论文，鸡巴论文奖那么多有什么用？你码代码给我看啊，我不知道数据是什么，不知道输入维度是什么，输出什么，里面到底发生了什么变化我怎么学？这就有点像，典型的在沙漠里教你钓鱼，在我看来，论文应该是最后才去看的东西。但是问题在于，你要有一个入门的教程啊。我想这是一个鸿沟，科研里面，理论和动手的鸿沟。<br>这篇教程就是引路人了。欢迎加入生成模型队伍。这个教程<strong>会一直保持更新</strong>，因为科技每天变幻莫测，同时我还会加入很多新内容，改进一些在以后看来是错误的说法。</p>
<p>首先，我们废话不多说了，直接show you the code：</p>
<figure class="highlight python"><table><tr><td class="code"><pre><div class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</div><div class="line"><span class="keyword">import</span> torch</div><div class="line"><span class="keyword">import</span> torch.nn <span class="keyword">as</span> nn</div><div class="line"><span class="keyword">import</span> torch.nn.functional <span class="keyword">as</span> F</div><div class="line"><span class="keyword">from</span> torch.autograd <span class="keyword">import</span> Variable</div><div class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</div><div class="line"><span class="keyword">from</span> scipy <span class="keyword">import</span> stats</div><div class="line"></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">generate_real_data_distribution</span><span class="params">(n_dim, num_samples)</span>:</span></div><div class="line">    all_data = []</div><div class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(num_samples):</div><div class="line">        x = np.random.uniform(<span class="number">0</span>, <span class="number">8</span>, n_dim)</div><div class="line">        y = stats.lognorm.pdf(x, <span class="number">0.6</span>)</div><div class="line">        all_data.append(y)</div><div class="line">    all_data = np.array(all_data)</div><div class="line">    print(<span class="string">'generated data shape: '</span>, all_data.shape)</div><div class="line">    <span class="keyword">return</span> all_data</div><div class="line"></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">batch_inputs</span><span class="params">(all_data, batch_size=<span class="number">6</span>)</span>:</span></div><div class="line">    <span class="keyword">assert</span> isinstance(all_data, np.ndarray), <span class="string">'all_data must be numpy array'</span></div><div class="line">    batch_x = all_data[np.random.randint(all_data.shape[<span class="number">0</span>], size=batch_size)]</div><div class="line">    <span class="keyword">return</span> Variable(torch.from_numpy(batch_x).float())</div><div class="line"></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></div><div class="line">    <span class="comment"># 给generator的噪音维数</span></div><div class="line">    n_noise_dim = <span class="number">30</span></div><div class="line">    <span class="comment"># 真实数据的维度</span></div><div class="line">    n_real_data_dim = <span class="number">256</span></div><div class="line">    num_samples = <span class="number">666</span></div><div class="line">    lr_g = <span class="number">0.001</span></div><div class="line">    lr_d = <span class="number">0.03</span></div><div class="line">    batch_size = <span class="number">6</span></div><div class="line">    epochs = <span class="number">1000</span></div><div class="line"></div><div class="line">    real_data = generate_real_data_distribution(n_real_data_dim, num_samples=num_samples)</div><div class="line">    print(<span class="string">'sample from real data: \n'</span>, real_data[: <span class="number">10</span>])</div><div class="line"></div><div class="line">    g_net = nn.Sequential(</div><div class="line">        nn.Linear(n_noise_dim, <span class="number">128</span>),</div><div class="line">        nn.ReLU(),</div><div class="line">        nn.Linear(<span class="number">128</span>, n_real_data_dim)</div><div class="line">    )</div><div class="line"></div><div class="line">    d_net = nn.Sequential(</div><div class="line">        nn.Linear(n_real_data_dim, <span class="number">128</span>),</div><div class="line">        nn.ReLU(),</div><div class="line">        nn.Linear(<span class="number">128</span>, <span class="number">1</span>),</div><div class="line">        nn.Sigmoid()</div><div class="line">    )</div><div class="line"></div><div class="line">    opt_d = torch.optim.Adam(d_net.parameters(), lr=lr_d)</div><div class="line">    opt_g = torch.optim.Adam(g_net.parameters(), lr=lr_g)</div><div class="line"></div><div class="line">    <span class="keyword">for</span> epoch <span class="keyword">in</span> range(epochs):</div><div class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> range(num_samples // batch_size):</div><div class="line">            batch_x = batch_inputs(real_data, batch_size)</div><div class="line">            batch_noise = Variable(torch.randn(batch_size, n_noise_dim))</div><div class="line"></div><div class="line">            g_data = g_net(batch_noise)</div><div class="line"></div><div class="line">            <span class="comment"># 用G判断两个输出分别多大概率是来自真正的画家</span></div><div class="line">            prob_fake = d_net(g_data)</div><div class="line">            prob_real = d_net(batch_x)</div><div class="line"></div><div class="line">            <span class="comment"># 很显然，mean里面的这部分是一个负值，如果想整体loss变小，必须要变成正直，加一个负号，否则会越来越大</span></div><div class="line">            d_loss = -torch.mean(torch.log(prob_real) + torch.log(<span class="number">1</span> - prob_fake))</div><div class="line">            <span class="comment"># 而g的loss要使得discriminator的prob_fake尽可能小，这样才能骗过它，因此也要加一个负号</span></div><div class="line">            g_loss = -torch.mean(torch.log(prob_fake))</div><div class="line"></div><div class="line">            opt_d.zero_grad()</div><div class="line">            d_loss.backward(retain_variables=<span class="keyword">True</span>)</div><div class="line">            opt_d.step()</div><div class="line"></div><div class="line">            opt_g.zero_grad()</div><div class="line">            g_loss.backward(retain_variables=<span class="keyword">True</span>)</div><div class="line">            opt_g.step()</div><div class="line"></div><div class="line">            print(<span class="string">'Epoch: &#123;&#125;, batch: &#123;&#125;, d_loss: &#123;&#125;, g_loss: &#123;&#125;'</span>.format(epoch, i, d_loss.data.numpy()[<span class="number">0</span>],</div><div class="line">                                                                        g_loss.data.numpy()[<span class="number">0</span>]))</div><div class="line"></div><div class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</div><div class="line">    main()</div></pre></td></tr></table></figure>
<p>这些代码，总共，也就是90行，核心代码50行，基本上，比你写一个其他程序都端，什么红黑算法，什么排序之类的。我个人比较喜欢简约，我很多时候不喜欢太鸡巴隆昌的代码。</p>
<h1 id="直接开始训练吧"><a href="#直接开始训练吧" class="headerlink" title="直接开始训练吧"></a>直接开始训练吧</h1><p>这个GAN很简单，三部分：</p>
<ul>
<li>real data生成，这个real data我们怎么去模拟呢？注意这里用的数据是二维的，不是图片，图片是三维的，二维你可以看成是csv，或者是序列，在这里面我们每一行，也就是一个样本，是sample自某个分布的数据，这里用的分布式lognorm；</li>
<li>d_net 和 g_net，这里两个net都是非常小，小到爆炸，这如果要是用tensorflow写就有点蛋疼了，我选择PyTorch，一目了然；</li>
<li>loss，loss在GAN中非常重要，是接下来的重点。</li>
</ul>
<p>OK，一阵复制粘贴，你就可以训练一个GAN，这个GAN用来做什么？就是你随机输入一个噪音，生成模型将会生成一个和lognorm分布一样的数据。也就是说，生成模型学到了lognrom分布。这能说明什么？神经网络学到了概率！用到图片里面就是，他知道哪个颜色快可能是什么东西，这也是现在的CycleGAN， DiscoGAN的原理。</p>
<h1 id="我吃饭去了"><a href="#我吃饭去了" class="headerlink" title="我吃饭去了"></a>我吃饭去了</h1><p>未完待续…</p>
<h1 id="来了"><a href="#来了" class="headerlink" title="来了"></a>来了</h1><p>继续刚才的，好像我写的文章没有人看啊，伤感。自己写自己看吧，哎，我骚味改了一下代码，loss函数部分，之前的写错了，我偷一张图把。<br><img src="http://wiseodd.github.io/img/2016-09-17-gan-tensorflow/algorithm.png" alt=""><br>这个是公式，原始GAN论文里面给的公式，但是毫无疑问，正如很多人说的那样，GAN很容易漂移：</p>
<figure class="highlight groovy"><table><tr><td class="code"><pre><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">66</span>, <span class="string">d_loss:</span> <span class="number">0.7026655673980713</span>, <span class="string">g_loss:</span> <span class="number">2.0336945056915283</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">67</span>, <span class="string">d_loss:</span> <span class="number">0.41225430369377136</span>, <span class="string">g_loss:</span> <span class="number">2.1994106769561768</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">68</span>, <span class="string">d_loss:</span> <span class="number">0.674636960029602</span>, <span class="string">g_loss:</span> <span class="number">1.5774009227752686</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">69</span>, <span class="string">d_loss:</span> <span class="number">0.5779278874397278</span>, <span class="string">g_loss:</span> <span class="number">2.2797725200653076</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">70</span>, <span class="string">d_loss:</span> <span class="number">0.4029145836830139</span>, <span class="string">g_loss:</span> <span class="number">2.200833559036255</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">71</span>, <span class="string">d_loss:</span> <span class="number">0.7264774441719055</span>, <span class="string">g_loss:</span> <span class="number">1.5658557415008545</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">72</span>, <span class="string">d_loss:</span> <span class="number">0.46858924627304077</span>, <span class="string">g_loss:</span> <span class="number">2.355680227279663</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">73</span>, <span class="string">d_loss:</span> <span class="number">0.6716371774673462</span>, <span class="string">g_loss:</span> <span class="number">1.7127293348312378</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">74</span>, <span class="string">d_loss:</span> <span class="number">0.7237206101417542</span>, <span class="string">g_loss:</span> <span class="number">1.4458404779434204</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">75</span>, <span class="string">d_loss:</span> <span class="number">0.9684935212135315</span>, <span class="string">g_loss:</span> <span class="number">1.943861961364746</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">76</span>, <span class="string">d_loss:</span> <span class="number">0.4705852270126343</span>, <span class="string">g_loss:</span> <span class="number">2.439894199371338</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">77</span>, <span class="string">d_loss:</span> <span class="number">0.4989328980445862</span>, <span class="string">g_loss:</span> <span class="number">1.5290288925170898</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">78</span>, <span class="string">d_loss:</span> <span class="number">0.44530192017555237</span>, <span class="string">g_loss:</span> <span class="number">2.9254989624023438</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">79</span>, <span class="string">d_loss:</span> <span class="number">0.6329593658447266</span>, <span class="string">g_loss:</span> <span class="number">1.7527830600738525</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">80</span>, <span class="string">d_loss:</span> <span class="number">0.42348209023475647</span>, <span class="string">g_loss:</span> <span class="number">1.856258749961853</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">81</span>, <span class="string">d_loss:</span> <span class="number">0.5396828651428223</span>, <span class="string">g_loss:</span> <span class="number">2.268836498260498</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">82</span>, <span class="string">d_loss:</span> <span class="number">0.9727945923805237</span>, <span class="string">g_loss:</span> <span class="number">1.0528483390808105</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">83</span>, <span class="string">d_loss:</span> <span class="number">0.7551510334014893</span>, <span class="string">g_loss:</span> <span class="number">1.508225917816162</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">84</span>, <span class="string">d_loss:</span> <span class="number">2.4204068183898926</span>, <span class="string">g_loss:</span> <span class="number">1.5375216007232666</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">85</span>, <span class="string">d_loss:</span> <span class="number">1.517686128616333</span>, <span class="string">g_loss:</span> <span class="number">0.6334291100502014</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">86</span>, <span class="string">d_loss:</span> inf, <span class="string">g_loss:</span> <span class="number">0.7990849614143372</span></div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">87</span>, <span class="string">d_loss:</span> nan, <span class="string">g_loss:</span> nan</div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">88</span>, <span class="string">d_loss:</span> nan, <span class="string">g_loss:</span> nan</div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">89</span>, <span class="string">d_loss:</span> nan, <span class="string">g_loss:</span> nan</div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">90</span>, <span class="string">d_loss:</span> nan, <span class="string">g_loss:</span> nan</div><div class="line"><span class="string">Epoch:</span> <span class="number">47</span>, <span class="string">batch:</span> <span class="number">91</span>, <span class="string">d_loss:</span> nan, <span class="string">g_loss:</span> nan</div></pre></td></tr></table></figure>
<p>你如果train一下的话会发现，到一定程度就会nan，这个nan我就无法理解了，按道理来说，从loss来看我们定义的来自以log，如果为无穷那么应该是log(0)了，但是我们的discriminator出来的函数是sigmoid啊，sigmoid不可能为0，只看是0-1且不包括闭区间。这个问题比较玄学。</p>
<p>既然nan的话，我也不深究是因为啥了，总之这个重点在于loss，因为后面GAN的变种基本上都是在loss的训练形式上。</p>
<h1 id="GAN-生成mnist"><a href="#GAN-生成mnist" class="headerlink" title="GAN 生成mnist"></a>GAN 生成mnist</h1><p>我们现在玩一下mnist把。</p>
<h1 id="交流"><a href="#交流" class="headerlink" title="交流"></a>交流</h1><p>我见了一个GAN群，加我微信让我拉进来。jintianiloveu, 顺便下载一个我做的app吧，内侧中，专门用来看美女图片的，你懂得。。<a href="http://www.jianshu.com/p/01e41d143e21" target="_blank" rel="noopener">传送门</a></p>

      
    </div>
    <footer class="article-footer">
      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '骚年，加个好友打赏一下啊，现在连泡面都吃不起了啊', // 可选参数，打赏标题
  btnText: '打赏支持', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: 'https://i.loli.net/2017/09/27/59cb048ba6838.jpeg',
  alipayImage: 'https://i.loli.net/2017/09/27/59cb049cd0951.jpeg'
});
</script>
      
      
        
	<div id="comment">
		<!-- 来必力City版安装代码 -->
		<div id="lv-container" data-id="city" data-uid="MTAyMC8zMDA5MC82NjQ1">
		<script type="text/javascript">
		   (function(d, s) {
		       var j, e = d.getElementsByTagName(s)[0];

		       if (typeof LivereTower === 'function') { return; }

		       j = d.createElement(s);
		       j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
		       j.async = true;

		       e.parentNode.insertBefore(j, e);
		   })(document, 'script');
		</script>
		<noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
		</div>
		<!-- City版安装代码已完成 -->
	</div>



      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2017/06/07/C-算法精研系列一/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">
        
          C++ 算法精研系列一
        
      </div>
    </a>
  
  
    <a href="/2017/05/29/萝莉萝莉1-0-2版本发布/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">萝莉萝莉1.0.2版本发布</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="toc-sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录</strong>
    
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#莫慌，50行代码你就可以成为无监督学习大牛"><span class="nav-number">1.</span> <span class="nav-text">莫慌，50行代码你就可以成为无监督学习大牛</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#直接开始训练吧"><span class="nav-number">2.</span> <span class="nav-text">直接开始训练吧</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#我吃饭去了"><span class="nav-number">3.</span> <span class="nav-text">我吃饭去了</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#来了"><span class="nav-number">4.</span> <span class="nav-text">来了</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#GAN-生成mnist"><span class="nav-number">5.</span> <span class="nav-text">GAN 生成mnist</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#交流"><span class="nav-number">6.</span> <span class="nav-text">交流</span></a></li></ol>
    
    </div>
  </aside>
</section>
        
      </div>
      
      <footer id="footer">
  

  <div class="container">
      	<div class="row">
	      <p> Powered by <a href="http://www.luoli-luoli.com/" target="_blank">萝莉萝莉</a> and <a href="http://www.luoli-luoli.com/sia" target="_blank">Sia</a> </p>
	      <p id="copyRightEn">Copyright &copy; 2017 - 2018 Jin Tian All Rights Reserved.</p>
	      
	      
    		<p class="busuanzi_uv">
				访客数 : <span id="busuanzi_value_site_uv"></span> |  
				访问量 : <span id="busuanzi_value_site_pv"></span>
		    </p>
  		   
		</div>

		
  </div>
</footer>


<!-- min height -->

<script>
    var wrapdiv = document.getElementById("wrap");
    var contentdiv = document.getElementById("content");
    var allheader = document.getElementById("allheader");

    wrapdiv.style.minHeight = document.body.offsetHeight + "px";
    if (allheader != null) {
      contentdiv.style.minHeight = document.body.offsetHeight - allheader.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    } else {
      contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    }
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
    <a href="http://luoli-luoli.com/chat" class="mobile-nav-link">Chat</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>




  <script src="/js/dialog.js"></script>








	<div style="display: none;">
    <script src="https://s95.cnzz.com/z_stat.php?id=1260716016&web_id=1260716016" language="JavaScript"></script>
  </div>



	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h2 class="modal-title" id="myModalLabel">设置</h2>
      </div>
      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">


      <div class="modal-body">
          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" onclick="javascript:setFontSize();" aria-expanded="true" aria-controls="collapseOne">
              正文字号大小
            </a>
          </div>
          <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
          <div class="panel-body">
            您已调整页面字体大小
          </div>
        </div>
      


          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" onclick="javascript:setBackground();" aria-expanded="true" aria-controls="collapseTwo">
              夜间护眼模式
            </a>
        </div>
          <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
          <div class="panel-body">
            夜间模式已经开启，再次单击按钮即可关闭 
          </div>
        </div>

        <div>
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="true" aria-controls="collapseThree">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关 于&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
        </div>
         <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
          <div class="panel-body">
            Jin Tian
          </div>
          <div class="panel-body">
            Copyright © 2018 Jintian All Rights Reserved.
          </div>
        </div>
      </div>


      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <div class="modal-footer">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
      </div>
    </div>
  </div>
</div>
  
  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js?v=1.0.0" async=""></script>
  
    <a id="menu-switch"><i class="fa fa-bars fa-lg"></i></a>
  
</body>
</html>